package com.star.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.star.pojo.Picture;
import com.star.pojo.dto.PictureReq;
import com.star.service.PictureService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * 照片墙后台管理控制器
 *
 * @author tanyapeng
 * @date 2021/2/5
 */
@Controller
@RequestMapping("/admin")
public class PictureController {

    @Resource
    private PictureService pictureService;

    /**
     * 查询照片列表
     *
     * @param model
     * @param pageNum
     * @return
     */
    @GetMapping("/pictures")
    public String pictures(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum, 10);
        List<Picture> listFriendLink = pictureService.listPicture();
        PageInfo<Picture> pageInfo = new PageInfo<Picture>(listFriendLink);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/pictures";
    }

    /**
     * 跳转新增页面
     *
     * @param model
     * @return
     */
    @GetMapping("/pictures/input")
    public String input(Model model) {
        model.addAttribute("picture", new Picture());
        return "admin/pictures-input";
    }

    /**
     * 新增照片
     *
     * @param res
     * @param result
     * @param attributes
     * @return
     * @throws IOException
     */
    @PostMapping("/pictures")
    public String post(PictureReq res, BindingResult result, RedirectAttributes attributes) throws IOException {

        if (result.hasErrors()) {
            return "admin/pictures-input";
        }

        int P = pictureService.savePicture(res);
        if (P == 0) {
            attributes.addFlashAttribute("message", "新增失败");
        } else {
            attributes.addFlashAttribute("message", "新增成功");
        }
        return "redirect:/admin/pictures";
    }

    /**
     * 跳转照片编辑页面
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/pictures/{id}/input")
    public String editInput(@PathVariable Long id, Model model) {
        model.addAttribute("picture", pictureService.getPicture(id));
        return "admin/pictures-input";
    }

    /**
     * 编辑相册
     *
     * @param picture
     * @param attributes
     * @return
     */
    @PostMapping("/pictures/{id}")
    public String editPost(Picture picture, RedirectAttributes attributes) {

        int P = pictureService.updatePicture(picture);
        if (P == 0) {
            attributes.addFlashAttribute("message", "编辑失败");
        } else {
            attributes.addFlashAttribute("message", "编辑成功");
        }
        return "redirect:/admin/pictures";
    }

    /**
     * 删除照片
     *
     * @param id
     * @param attributes
     * @return
     */
    @GetMapping("/pictures/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes) {
        pictureService.deletePicture(id);
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin/pictures";
    }

}